package com.todo.event.activity.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.event.activity.common.enums.EventActivityStatusEnum;
import com.todo.event.activity.infrastructure.entity.condition.EventActivityCondition;
import com.todo.event.activity.infrastructure.entity.po.EventActivityPO;
import com.todo.event.activity.infrastructure.mapper.EventActivityMapper;
import com.todo.event.activity.infrastructure.repository.EventActivityRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/1/7 09:26
 * @description
 */
@Slf4j
@Repository
public class EventActivityRepositoryImpl implements EventActivityRepository {

    @Resource
    private EventActivityMapper eventActivityMapper;

    @Override
    public boolean insertEventActivity(EventActivityPO eventActivityPO) {
        if (eventActivityPO == null) {
            return false;
        }
        if (eventActivityPO.getName() == null || eventActivityPO.getDecisionId() == null || eventActivityPO.getStartTime() == null || eventActivityPO.getEndTime() == null) {
            log.info("EventActivityRepository#insertEventActivity 活动数据操作失败，基础数据为空 eventActivityPO：【{}】", JSONObject.toJSONString(eventActivityPO));
            return false;
        }
        if (eventActivityPO.getStartTime().after(eventActivityPO.getEndTime())) {
            log.info("EventActivityRepository#insertEventActivity 活动数据操作失败，开始时间大于结束时间 eventActivityPO：【{}】", JSONObject.toJSONString(eventActivityPO));
            return false;
        }
        return eventActivityMapper.insertEventActivity(eventActivityPO) > 0;
    }

    @Override
    public boolean updateEventActivity(EventActivityPO eventActivityPO) {
        if (eventActivityPO == null || eventActivityPO.getId() == null) {
            return false;
        }
        return eventActivityMapper.updateEventActivity(eventActivityPO) > 0;
    }

    @Override
    public boolean removeEventActivityById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        return eventActivityMapper.removeEventActivityById(id) > 0;
    }

    @Override
    public EventActivityPO queryEventActivityById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return eventActivityMapper.queryEventActivityById(id);
    }

    @Override
    public List<EventActivityPO> queryEventActivityListByDecisionId(Long decisionId) {
        if (decisionId == null || decisionId < 0) {
            return Collections.emptyList();
        }
        return eventActivityMapper.queryEventActivityListByDecisionId(decisionId, new Date());
    }

    @Override
    public EventActivityPO queryEventActivityByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return eventActivityMapper.queryEventActivityByName(name);
    }

    @Override
    public boolean updateEventActivityStatus(Long id, EventActivityStatusEnum oldStatus, EventActivityStatusEnum newStatus) {
        if (id == null || id < 0 || oldStatus == null || newStatus == null) {
            return false;
        }
        return eventActivityMapper.updateEventActivityStatus(id, oldStatus.getType(), newStatus.getType()) > 0;
    }

    @Override
    public PageResult<EventActivityPO> queryEventActivityByCondition(EventActivityCondition eventActivityCondition) {
        PageResult<EventActivityPO> pageResult = new PageResult<>();
        if (eventActivityCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(eventActivityCondition.getPageIndex());
        pageResult.setPageSize(eventActivityCondition.getPageSize());
        long count = eventActivityMapper.queryCountByCondition(eventActivityCondition);
        if (count <= 0) {
            log.info("EventActivityRepository#queryEventActivityByCondition 活动数据查询完成，总数为空 eventActivityCondition：【{}】", JSONObject.toJSONString(eventActivityCondition));
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, eventActivityCondition.getPageSize()));
        pageResult.setTotal(count);
        List<EventActivityPO> eventActivityPOList = eventActivityMapper.queryEventActivityListByCondition(eventActivityCondition);
        if (CollectionUtils.isEmpty(eventActivityPOList)) {
            pageResult.setHasNext(false);
            log.info("EventActivityRepository#queryEventActivityByCondition 活动数据查询完成，查询结果数据为空 eventActivityCondition：【{}】", JSONObject.toJSONString(eventActivityCondition));
            return pageResult;
        }
        pageResult.setResults(eventActivityPOList);
        pageResult.setCurrentIndex(eventActivityPOList.get(0).getId());
        pageResult.setHasNext(eventActivityPOList.size() >= eventActivityCondition.getPageSize());
        return pageResult;
    }
}
